package com.byapp.fast.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class IPUtils {

    /**
     * 获取IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("X-Forwarded-For");
            if ((StringUtils.isBlank(ip)) || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("X-Real-IP");
            }
            if ((StringUtils.isBlank(ip)) || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if ((StringUtils.isBlank(ip)) || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if ((StringUtils.isBlank(ip)) || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if ((StringUtils.isBlank(ip)) || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if ((StringUtils.isBlank(ip)) || ("unknown".equalsIgnoreCase(ip))) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            log.error("IPUtils Error ", e);
        }
        return ip;
    }

}
